package com.hnust.root.service.forword.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnust.root.common.util.ComUtil;
import com.hnust.root.dao.entity.domain.forward.Comment;
import com.hnust.root.dao.entity.domain.forward.Reply;
import com.hnust.root.dao.mapper.wx.CommentMapper;
import com.hnust.root.dao.mapper.wx.ReplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplyService {
    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private WxUserInfoService userInfoService;


    //添加回复
    public boolean addReply(Reply reply) {
        Comment comment = commentMapper.selectById(reply.getCommentId());
        reply.setToUser(comment.getOpenId());

        comment.setReplyCount(comment.getReplyCount() + 1);
        commentMapper.updateById(comment);
        return ComUtil.DBisSuccess(replyMapper.insert(reply));
    }

    //通过评论ID获取回复列表
    public List<Reply> getReplyList(int commentId) {
        List<Reply> replyList = replyMapper.selectList(new QueryWrapper<Reply>().eq("comment_id", commentId));
        replyList.forEach(reply -> {
            reply.setFromUserInfo(userInfoService.getUserInfoByOpenId(reply.getFromUser()));
            reply.setToUserinfo(userInfoService.getUserInfoByOpenId(reply.getToUser()));
        });
        return replyList;
    }

    public Reply getReplyById(int id) {
        return replyMapper.selectById(id);
    }

    //通过话题ID删除回复记录
    public void deleteReplyByTopicId(int topicId) {
        replyMapper.delete(new QueryWrapper<Reply>().eq("topic_id", topicId));
    }
}
